﻿// Propeller RPM and Hobbs Meter- 引擎转速表

#include "PropellerHobbs.h"

static int r = 250;
static int X = 4 * r;//窗口长
static int Y = 3 * r; //窗口宽
static int x = 2 * r;//盘的横坐标
static int y = 8 * r / 5; //盘的纵坐标

void drawPHI(int x0, int y0, int r0, double engineRPM)
{
	//setinitmode(0, x, y);//(0,x,y)
	//setcaption("转速表");//窗口名称
	//initgraph(X, Y, INIT_RENDERMANUAL);
	//setbkcolor(WHITE);
	x = x0;
	y = y0;
	r = r0;
	ege_enable_aa(true); //开启抗锯齿
	double angle = engineRPM - 13;
	if (angle <= 0) {
		angle = 0;
	}
	double radin = (angle /10*260/35) * PI / 180 - PI*5/4;
	//for (; is_run(); delay_fps(30)) {
	//	cleardevice();
	settextjustify(LEFT_TEXT, TOP_TEXT);
	draw_kuangPHI();
	draw_frameworkPHI();
	numberPanelPHI();
	draw_pointerPHI(radin);
	screwPHI();
	//	//shadow();
	//	if (kbhit()) { // 检测是否有按键被按下
	//		int key = getch(); // 获取按下的键值
	//		switch (key) {
	//		case 'W': // 上箭头键
	//			radin += 0.8 * PI / 180; // 增加角度
	//			break;
	//		case 'S': // 下箭头键
	//			radin -= 0.8 * PI / 180; // 减少角度
	//			break;
	//		case 'A': // 左箭头键
	//			radin -= 0.8 * PI / 180; // 逆时针旋转
	//			break;
	//		case 'D': // 右箭头键
	//			radin += 0.8 * PI / 180; // 顺时针旋转
	//			break;
	//		case 27:
	//			return 0;
	//		}
	//	}
	//	//getch();
	//	if (radin > 2 * PI) radin -= 2 * PI;
	//	if (radin < 0) radin += 2 * PI;
	//}
	//closegraph();
	//return 0;
}
void draw_frameworkPHI() {
	//画主要的圆盘
	setcolor(EGEARGB(42, 42, 42, 42));
	setfillcolor(EGEARGB(42, 42, 42, 42));//填充的颜色

	/*setcolor(BLACK);
	circle(x, y, r / 10);
	setfillcolor(BLACK);*/

	//灰色下
	setlinewidth(14 * r / 250.0);
	setcolor(EGEARGB(255, 49, 215, 46));
	//x - r * 0.95, y - r * 0.95, 1.9 * r, 1.9 * r, 57, 68
	ege_arc(x - r * 0.8, y - r * 0.8, 1.6 * r, 1.6 * r, -77, 53);
	setlinewidth(20 * r / 250.0);
	ege_arc(x - r * 0.71, y - r * 0.71, 1.42 * r, 1.42 * r, -77, 37);
	setlinewidth(7 * r / 250.0);
	ege_arc(x - r * 0.76, y - r * 0.76, 1.52 * r, 1.52 * r, -77, 44);



	setcolor(WHITE);//刻度颜色
	//绘制刻度
	int numTicks = 8; // 刻度数量
	double angleStep = 260.0 / (numTicks - 1); // 每个刻度之间的角度
	double startAngle = 135; // 起始角度
	//画粗刻度
	for (int i = 0; i < numTicks; i++)
	{
		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.6725 * cos(radian));
		double startY = y + (r * 0.6725 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(6 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	numTicks = 36; // 刻度数量
	angleStep = 260.0 / (numTicks - 1); // 每个刻度之间的角度
	startAngle = 135; // 起始角度
	//画细刻度
	for (int i = 0; i < numTicks; i++)
	{
		if (i % 5 == 0) continue;

		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.77 * cos(radian));
		double startY = y + (r * 0.77 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		if (i == 27) {
			//特殊刻度
			setcolor(RED);
			setlinewidth(7 * r / 250.0);
			ege_line(startX - (r * 0.08 * cos(radian)), startY - (r * 0.08 * sin(radian)), endX, endY);
			setcolor(WHITE);
			setlinewidth(3 * r / 250.0);
			continue;
		}
		setlinewidth(3 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	//绘制刻度的变量
	setbkmode(TRANSPARENT);//透明字体背景
	setfont(r / 9, 0, "Arial");//字体
	settextcolor(WHITE);
	ege_outtextxy(x - r / 12, y - r / 2.6, "RPM");
	setfont(r / 11, 0, "Arial");//字体
	ege_outtextxy(x - r / 7, y + r / 2, "HOURS");
	setfont(r / 10.5, 0, "Arial");//字体
	ege_outtextxy(x - r / 14, y - r / 4, "X100");

	//绘制数字
	setfont(r / 5, 0, "Arial");
	ege_outtextxy(x - r / 1.6, y - r / 80, "5");
	ege_outtextxy(x - r / 1.7, y - r / 3, "10");
	ege_outtextxy(x - r / 3, y - r / 1.7, "15");
	ege_outtextxy(x + r / 20, y - r / 1.67, "20");

	ege_outtextxy(x + r / 3, y - r / 2.4, "25");
	ege_outtextxy(x + r / 2.1, y - r / 10, "30");
}

void draw_pointerPHI(double radin) {
	setfillcolor(WHITE);
	ege_circle(x, y, r / 25);
	ege_fillcircle(x, y, r / 25);
	ege_point polypoint[] = { x + r / 1.3 * cos(radin) ,y + r / 1.3 * sin(radin), x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180), x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180), x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180) };
	ege_fillpoly(4, polypoint);
	setfillcolor(GRAY);
	ege_point polypoint1[] = { x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180),
		x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180),
		x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180),
		x + r / 6.8 * cos(radin - 210 * PI / 180),y + r / 6.8 * sin(radin - 210 * PI / 180),
		x + r / 5 * cos(radin - 180 * PI / 180), y + r / 5 * sin(radin - 180 * PI / 180),
		x + r / 6.8 * cos(radin - 150 * PI / 180),y + r / 6.8 * sin(radin - 150 * PI / 180), };
	ege_fillpoly(6, polypoint1);
}

void draw_kuangPHI() {

	int circle_r = r;
	int circle_x = x;
	int circle_y = y;

	setcolor(BLACK);//描边颜色
	setlinewidth(15 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边

	ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 };
	ege_setpattern_ellipsegradient(p5, EGERGB(160, 160, 160), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGEACOLOR(0xFF, 0x00161616));
	ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

	//最外圈

	setcolor(BLACK);//描边颜色
	setlinewidth(2 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r * 3 / 150);//描边

	ege_point p6 = { circle_x - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 };
	ege_setpattern_ellipsegradient(p6, EGERGB(200, 200, 200), circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6), EGEACOLOR(0xFF, 0x003A3A3C));
	ege_fillrect(circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6));
	//第二圈

	setlinewidth(14 * r / 250.0);//线条粗细
	setfillcolor(EGERGB(50, 50, 50));//填充为灰色
	ege_circle(circle_x, circle_y, circle_r - circle_r / 30);//描边

	ege_point p7 = { circle_x  ,circle_y };
	ege_setpattern_ellipsegradient(p7, EGERGB(70, 70, 70), circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30), EGEACOLOR(0xFF, 0x00292925));
	ege_fillrect(circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30));


	ege_fillcircle(circle_x, circle_y, circle_r - circle_r / 30);
	setfillcolor(EGERGB(38, 38, 38));//填充为深灰色
}

void screwPHI()//螺丝
{
	double DIAL_RADIUS = r;
	double CENTER_X = x;
	double CENTER_Y = y;

	// 使用float保证计算精度（根据需求可改为double）
	const float DIAL_RADIUS_F = (float)DIAL_RADIUS;
	const float CENTER_X_F = (float)CENTER_X;
	const float CENTER_Y_F = (float)CENTER_Y;

	// 参数化定义（精确到小数点后6位）
	const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
	const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
	const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
	const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
	const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
	const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
	const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
	const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
	const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
	const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
	const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

	ege_enable_aa(TRUE);

	// 绘制函数（使用浮点坐标）
	auto draw_screw = [&](float cx, float cy) {
		// 精确到小数点后6位的渐变参数
		ege_setpattern_ellipsegradient(
			ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
			EGEACOLOR(0xFF, BLACK),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(ELLIPSE_SIZE),
			roundf(ELLIPSE_SIZE),
			EGEACOLOR(0, WHITE)
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 灰色渐变层（精确尺寸调整）
		const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
		ege_setpattern_ellipsegradient(
			ege_point{
			  roundf(cx - SCREW_OFFSET * 0.5f),
				roundf(cy - SCREW_OFFSET * 0.5f)
			},
			EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(adjusted_size),
			roundf(adjusted_size),
			EGEACOLOR(0xFF, EGERGB(20, 20, 20))
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 十字线（亚像素级精度）
		setcolor(EGERGB(30, 30, 30));
		setlinewidth(roundf(LINE_WIDTH));

		const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
		ege_line(
			roundf(cx - CROSS_OFFSET1), roundf(cross_y),
			roundf(cx + CROSS_OFFSET1), roundf(cross_y)
		);

		const float cross_x = cx;
		const float cross_y_start = cy + CROSS_OFFSET2 / 4;
		const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
		ege_line(
			roundf(cross_x), roundf(cross_y_start),
			roundf(cross_x), roundf(cross_y_end)
		);
		};

	// 计算四个螺丝中心坐标（浮点运算）
	const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

	// 左上
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右上
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右下
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);

	// 左下（新增）
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);
}//螺丝

void numberPanelPHI() {
	setlinewidth(0.01 * r / 250.0);
	setcolor(EGEARGB(255, 122, 122, 122));
	ege_rectangle(x - r / 2, y + r / 6, r, r / 4);
	ege_fillrect(x - r / 2, y + r / 6, r, r / 4);

	setfillcolor(EGEARGB(255, 0, 0, 0));
	ege_rectangle(x - r / 2.4, y + r / 5.5, r / 1.2, r / 4.8);
	ege_fillrect(x - r / 2.4, y + r / 5.5, r / 1.2, r / 4.8);

	setfillcolor(EGEARGB(255, 25, 25, 25, 25));
	ege_point polypoints1[8] = { x - r / 2, y + r / 6,x + r / 2,y + r / 6, x + r / 2.4, y + r / 5.5,x - r / 2.4, y + r / 5.5, };
	ege_fillpoly(4, polypoints1);

	setfillcolor(EGEARGB(255, 25, 25, 25, 25));
	ege_point polypoints2[8] = { x - r / 2, y + r / 6 + r / 4,x + r / 2,y + r / 6 + r / 4, x + r / 2.4, y + r / 5.5 + r / 4.8,x - r / 2.4, y + r / 5.5 + r / 4.8 };
	ege_fillpoly(4, polypoints2);
	//绘制数字
	setcolor(WHITE);
	ege_outtextxy(x - r / 2.8, y + r / 5.5, "0");

	ege_outtextxy(x - r / 2.8 + r / 6, y + r / 5.5, "0");
	ege_outtextxy(x - r / 2.8 + 2 * r / 6, y + r / 5.5, "0");
	ege_outtextxy(x - r / 2.8 + 3 * r / 6, y + r / 5.5, "0");
	ege_outtextxy(x - r / 2.8 + 4 * r / 6, y + r / 5.5, "1");
	setcolor(EGEARGB(255, 122, 122, 122));
	ege_line(x - r / 2.6 + r / 6, y + r / 5.5, x - r / 2.6 + r / 6, y + r / 2.6);
	ege_line(x - r / 2.6 + 2 * r / 6, y + r / 5.5, x - r / 2.6 + 2 * r / 6, y + r / 2.6);
	ege_line(x - r / 2.6 + 3 * r / 6, y + r / 5.5, x - r / 2.6 + 3 * r / 6, y + r / 2.6);
	ege_line(x - r / 2.6 + 4 * r / 6, y + r / 5.5, x - r / 2.6 + 4 * r / 6, y + r / 2.6);
}